import { Component, OnInit, Input, Injector } from '@angular/core';
import { NgZorroPageBaseComponent } from '../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component';
import { HttpService } from "../../core/service/http.service";
import { Router, Route, ActivatedRoute } from "@angular/router";
import {
    AndOr, Compare, ExpressionPair, PagedResultDto, PagerSearchInput, ElementViewType, BaseComponentConfig, SonPagerSearchInput,
    SystemFormControl, FieldType, CustomNavigation, ElementShowType, KeyValueOutput, SystemFormControlOutput,
    DrillRoleOutput,
    DrillGroup
} from "../..";
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import {
    FormBuilder, FormGroup, FormsModule, ReactiveFormsModule, Validators,
    FormControl
} from "@angular/forms";
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { isNumber } from 'util';

@Component({
    selector: 'app-course-student',
    templateUrl: './course-student.component.html',
    styleUrls: ['../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component.scss']
})
export class CourseStudentComponent extends NgZorroPageBaseComponent implements OnInit {
    /**
 * 是否从远程获取数据
 */
    isLoading = false;
    get studentFilter(): string {
        return this.listSearchValue[0] && this.listSearchValue[0].propertyValue ? this.listSearchValue[0].propertyValue : '';
    }
    set studentFilter(value: string) {
        if (this.listSearchValue.length === 0) {
            this.listSearchValue = new Array<ExpressionPair>(
                new ExpressionPair('ApplyType', '', Compare.Equal, AndOr.And),
            );
        }
        this.listSearchValue[0].propertyValue = value;
        this.getEntityList();
    }
    constructor(injector: Injector) {
        super(injector,
            new BaseComponentConfig(
                "Course/GetStudentsByCourse",
                "",
                "",
                "",
                "",
                "id",
                "",
                "",
                null,
                "courseInfoId"
            ));

    }


    async joinCourse(value: string | number, opType: number) {
        this.loadingData = true;
        try {
            if (isNumber(value)) {

                try {
                    await this.httpService.post('Course/ChangeJoinType', {
                        "id": this._pKeyFieldValue,
                        "ids": [value],
                        applyType: opType,
                    });
                    await this.getEntityList();
                    this.refreshStatus();
                } catch (error) {
                }
            } else {
                const thisInstance = this;
                const itemKey = [];
                this.listEntities.entities.filter(e => e.IsChecked).forEach(e => {
                    itemKey.push(e[this.componentConfig.KeyFieldName]);
                });
                const body = {
                    "id": this._pKeyFieldValue,
                    "ids": itemKey,
                    applyType: opType,
                };
                this.nzModalService.confirm({
                    nzTitle: '您是否要加入选中的课程',
                    nzContent: `已经选中的：${itemKey.length}项内容？`,
                    nzZIndex: 1010,
                    nzOnOk() {
                        return new Promise(async (resolve, reject) => {
                            try {
                                await thisInstance.httpService.post('Course/ChangeJoinType', body);
                                await thisInstance.getEntityList();
                                thisInstance.refreshStatus();
                                resolve();
                            } catch (error) {
                                reject();
                            }
                        }).catch(e => {
                        });
                    }
                });
            }
        } catch (error) {
            this.isLoading = false;
        }

    }

    getNewEntity(): DrillGroup {
        return new DrillGroup();
    }

    navigateCustom(url: string, pid: any) {
        throw new Error("Method not implemented.");
    }

    showModalForComponent() {
        throw new Error("Method not implemented.");
    }
}
